package com.toune.movie.mvp.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.AVUtils;
import com.avos.avoscloud.DeleteCallback;
import com.avos.avoscloud.FindCallback;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.siberiadante.customdialoglib.CustomDialog;
import com.siberiadante.customdialoglib.EnsureDialog;
import com.toune.movie.R;
import com.toune.movie.base.AppConstant;
import com.toune.movie.base.BaseActivity;
import com.toune.movie.base.myUtil.MyUtil;
import com.toune.movie.base.myview.MusicPlayDialog;
import com.toune.movie.mvp.adapter.AdapterDownHis;
import com.toune.movie.mvp.adapter.AdapterHistory;
import com.toune.movie.mvp.contract.DownListActivityContract;
import com.toune.movie.mvp.presenter.DownListActivityPresenter;
import com.toune.movie.vo.MusicVo;
import com.toune.movie.vo.ShareVo;
import com.vondear.rxtool.RxDeviceTool;
import com.vondear.rxtool.RxFileTool;
import com.vondear.rxtool.view.RxToast;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import gdut.bsx.share2.ShareContentType;

import static com.toune.movie.base.myUtil.MyUtil.musicPlayDialog;

public class DownListActivity extends BaseActivity<DownListActivityPresenter> implements DownListActivityContract.View {
    @BindView(R.id.mLreclView)
    RecyclerView mLreclView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private AdapterDownHis adapterDownHis;
    private String localPath;

    @Override // 实例化presenter
    public DownListActivityPresenter initPresenter() {
        return new DownListActivityPresenter();
    }

    @Override
    public void loadMore() {

    }

    @Override
    public void refreData() {
        getDatas();
    }

    private List<AVObject> allList = new ArrayList<>();
    private List<MusicVo.DataBean> allMusicList = new ArrayList<>();

    @Override
    public void init(Bundle savedInstanceState) {
        getDatas();
    }

    private void getDatas() {
        AVQuery<AVObject> avQuery = new AVQuery<>(AppConstant.TAB_DOWN_HIS);
        if (AVUser.getCurrentUser() != null) {
            avQuery.whereMatches("userId", AVUser.getCurrentUser().getObjectId());
        } else {
            avQuery.whereMatches("phoneId", RxDeviceTool.getUniqueSerialNumber());
        }
        avQuery.findInBackground(new FindCallback<AVObject>() {
            @Override
            public void done(List<AVObject> list, AVException e) {
                if (e == null) {
                    allList = list;
                    for (int i = 0; i < allList.size(); i++) {
                        AVObject item = allList.get(i);
                        MusicVo.DataBean dataBean = new MusicVo.DataBean();
                        dataBean.setAuthor(item.getString("authorName"));
                        dataBean.setTitle(item.getString("fileName"));
                        dataBean.setUrl(item.getString("url"));
                        dataBean.setPic(item.getString("imgUrl"));
                        dataBean.setLrc(item.getString("lrc"));
                        dataBean.setLink(item.getString("link"));
                        dataBean.setSongid(item.getString("songId"));
                        allMusicList.add(dataBean);
                    }
                    notifyAdapter();
                }
            }
        });
    }

    public MusicPlayDialog musicPlayDialog;

    private void notifyAdapter() {
        if (adapterDownHis == null) {
            adapterDownHis = new AdapterDownHis(R.layout.adapter_down_his, allList);
            mLreclView.setLayoutManager(new LinearLayoutManager(context));
            mLreclView.setAdapter(adapterDownHis);
            adapterDownHis.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
                @Override
                public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
                    //长按删除弹框
                    deleteDialog(allList.get(position));
                    return true;
                }
            });
            adapterDownHis.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    MusicVo.DataBean dataBean = allMusicList.get(position);
                    localPath = allList.get(position).getString("localPath");
                    musicPlayDialog = MyUtil.showMusicPlayDialog(context, dataBean, localPath, position);
                    if (musicPlayDialog != null) {
                        musicPlayDialog.setOnSwitchListener(new MusicPlayDialog.OnSwitchListener() {
                            @Override
                            public void onLeft(int pos) {
                                if (musicPlayDialog == null) {
                                    return;
                                }
                                if (pos > 0) {
                                    int position = pos - 1;
                                    musicPlayDialog.resetMusic(allMusicList.get(position), localPath, position);
                                } else {
                                    int position = allMusicList.size() - 1;
                                    musicPlayDialog.resetMusic(allMusicList.get(position), localPath, position);
                                }
                            }

                            @Override
                            public void onRight(int pos) {
                                if (musicPlayDialog == null) {
                                    return;
                                }
                                if (allMusicList.size() - 1 > pos) {
                                    int position = pos + 1;
                                    musicPlayDialog.resetMusic(allMusicList.get(position), localPath, position);
                                } else {
                                    musicPlayDialog.resetMusic(allMusicList.get(0), localPath, 0);
                                }
                            }
                        });
                    }
                }
            });
        } else {
            adapterDownHis.notifyDataSetChanged();
        }
    }

    private void deleteDialog(final AVObject avObject) {
        CustomDialog customDialog = new CustomDialog(context, R.layout.dialog_music_del, new int[]{R.id.share_tv, R.id.del_tv, R.id.re_down_tv});
        customDialog.setOnDialogItemClickListener(new CustomDialog.OnCustomDialogItemClickListener() {
            @Override
            public void OnCustomDialogItemClick(CustomDialog customDialog, View view) {
                switch (view.getId()) {
                    case R.id.del_tv:
                        avObject.deleteInBackground(new DeleteCallback() {
                            @Override
                            public void done(AVException e) {
                                if (e == null) {
                                    allList.remove(avObject);
                                    notifyAdapter();
                                } else {
                                    RxToast.error(e.getMessage());
                                }
                            }
                        });
                        break;
                    case R.id.re_down_tv:
                        MusicVo.DataBean dataBean = new MusicVo.DataBean();
                        dataBean.setAuthor(avObject.getString("authorName"));
                        dataBean.setTitle(avObject.getString("fileName"));
                        dataBean.setUrl(avObject.getString("url"));
                        dataBean.setSongid(avObject.getString("songId"));
                        dataBean.setLink(avObject.getString("link"));
                        dataBean.setLrc(avObject.getString("lrc"));
                        dataBean.setPic(avObject.getString("imgUrl"));
                        MyUtil.showDownDialog(context, dataBean);
                        break;
                    case R.id.share_tv:
                        ShareVo shareVo = new ShareVo();
                        shareVo.setShareType(ShareContentType.AUDIO);
                        shareVo.setFileName(avObject.getString("fileName"));
                        shareVo.setUrl(avObject.getString("localPath"));
                        shareVo.setTitle("快来和我一起听歌...");
                        shareVo.setImagUrl(avObject.getString("imgUrl"));
                        if (RxFileTool.isFileExists(shareVo.getUrl())) {
                            MyUtil.showShareDialog(context, shareVo);
                        } else {
                            RxToast.info("本地文件不存在，请重新下载后再分享。");
                        }
                        break;
                }
            }
        });
        customDialog.show();
    }


    @Override
    protected int getLayout() {
        return R.layout.activity_down_list;
    }

    @Override
    protected String getTitleStr() {
        return "下载记录";
    }

    @Override
    protected void initEventAndData() {

    }

    @Override
    public void showError(String msg) {

    }

    @Override
    public void useNightMode(boolean isNight) {

    }
}
